﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.DataModel.Report;
using Microsoft.Reporting.WebForms;
using NPMonitor.Implementation;

namespace NPMonitor.Web.UserControls
{
    public partial class ReportControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                System.Collections.ArrayList listTable = new System.Collections.ArrayList();
                System.Collections.ArrayList listChar = new System.Collections.ArrayList();
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource rds = new ReportDataSource("dsChart", listChar);
                ReportViewer1.LocalReport.DataSources.Add(rds);

                ReportDataSource rdsTable = new ReportDataSource("dsTable", listTable);
                ReportViewer1.LocalReport.DataSources.Add(rdsTable);
                ReportViewer1.Visible = false;
            }
        }

        public void BindReportData(List<ReportData> list, ReportRefrenceData baseLine, string reportPath ,string reportTime,string reportTitle)
        {
            if (list == null || list.Count == 0)
                return;
            ReportViewer1.Visible = true;
            ReportViewer1.LocalReport.ReportPath = reportPath;
            System.Collections.ArrayList listTable = new System.Collections.ArrayList();
            System.Collections.ArrayList listChar = new System.Collections.ArrayList();

            if (list.Count > 0)
            {
                foreach (ReportData data in list)
                {
                    if (data.list.Count > 0)
                    {
                        foreach (ChartPoint info in data.list)
                        {
                            var model = new DataModelTemp();
                            model.Legend = data.Legend;
                            model.ScenarioNO = data.Group;
                            model.BeginTime = info.BeginTime;
                            model.AverageTime = info.AverageTime;
                            listTable.Add(model);
                            listChar.Add(model);
                        }
                    }
                }

                if (baseLine != null && baseLine.RefrenceTime > 0)
                {
                    
                    foreach (var item in list[0].list)
                    {
                        DataModelTemp baseLineChart = new DataModelTemp();
                        baseLineChart.Legend = baseLine.Legend;
                        baseLineChart.ScenarioNO = list[0].Group;
                        baseLineChart.BeginTime = item.BeginTime;
                        baseLineChart.AverageTime = baseLine.RefrenceTime;
                        listChar.Add(baseLineChart);
                    }
                }
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("RPTitle", reportTitle));
                ReportViewer1.LocalReport.SetParameters(new ReportParameter("RPTime", reportTime));

                this.ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.EnableHyperlinks = true; // if there is URL links in your RDLC, this is need
                ReportViewer1.LocalReport.DataSources.Clear();
                // Generate data automatically
                ReportDataSource rds = new ReportDataSource("dsChart", listChar);
                ReportViewer1.LocalReport.DataSources.Add(rds);

                ReportDataSource rdsTable = new ReportDataSource("dsTable", listTable);
                ReportViewer1.LocalReport.DataSources.Add(rdsTable);
                ReportViewer1.LocalReport.Refresh();
            }
        }
    }

    public class DataModelTemp
    {
        public string Legend { get; set; }
        public string ScenarioNO { get; set; }
        public string BeginTime { get; set; }
        public decimal AverageTime { get; set; }
    }
}